/**
 * 
 */
package com.yinhai.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.yinhai.adapter.NewsContentListAdapter;
import com.yinhai.cfg.LoginConfig;
import com.yinhai.cfg.LoginConfigUtil;
import com.yinhai.constant.UrlConstant;
import com.yinhai.model.NewsContent;
import com.yinhai.model.NewsContentList;
import com.yinhai.service.NoticeNewsServiceImpl;

/**
 * @author lz
 * 
 * 
 */
public class NewsContentActivity extends Activity {
	public String phone;
	private ImageView news_btn;
	private AQuery aq, aq1 = null;
	LinearLayout layoutll = null;
	NoticeNewsServiceImpl noticedao = null;
	NewsContentActivity thisRef = null;
	private List<String> list = null;
	private List<NewsContentList> listcont = null;

	NewsContentListAdapter listadpter = null;
	private int Isfind = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_news);
		thisRef = this;
		aq = new AQuery(thisRef);
		aq1 = new AQuery(thisRef);
		LoginConfig log = LoginConfigUtil.getConfig(thisRef);
		phone = log.getUsername();

		listadpter = new NewsContentListAdapter(thisRef);
		aq.id(R.id.message_list).adapter(listadpter);
		noticedao = new NoticeNewsServiceImpl(thisRef);

		findNotices();

		news_btn = (ImageView) findViewById(R.id.news_btn);
		news_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});

	}

	private void findNotices() {
		list = new ArrayList<String>();
		String url = UrlConstant.LOCAL_URI + "findNotices";
		Map<String, String> params = new HashMap<String, String>();
		params.put("phone", phone);
		aq.ajax(url, params, JSONObject.class, new AjaxCallback<JSONObject>() {
			@Override
			public void callback(String url, JSONObject json, AjaxStatus status) {
				try {
					JSONArray js;
					js = json.getJSONArray("data");
					for (int i = 0; i < js.length(); i++) {
						String name = js.get(i) + "";
						list.add(name);
					}
					findById();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private void findById() {
		listcont = new ArrayList<NewsContentList>();
		List<NewsContent> list1 = noticedao.findByList();
		Map<String, String> map = new HashMap<String, String>();
		for (int i = 0; i < list1.size(); i++) {
			map.put(list1.get(i).getUuid(), list1.get(i).getUuid());
		}
		for (int i = 0; i < list.size(); i++) {
			if (map.remove(list.get(i)) != null) { // 存在这条信息
				// NewsContentList ul =new NewsContentList();
				// ul.setId(i+"");
				// ul.setIsd(0) ;// 0 , 为本地 1 网络
				// listcont.add(ul);
			} else {// 不存在
					// NewsContentList ul =new NewsContentList();
				// ul.setId(i+"");
				// ul.setIsd(1) ;// 0 , 为本地 1 网络
				// listcont.add(ul);

				String url = UrlConstant.LOCAL_URI + "findNoticeById";
				Map<String, String> params = new HashMap<String, String>();
				params.put("noticeId", list.get(i));
				aq1.ajax(url, params, JSONObject.class,
						new AjaxCallback<JSONObject>() {
							@Override
							public void callback(String url, JSONObject js,
									AjaxStatus status) {
								if (js != null) {
									try {
										NewsContent u = new NewsContent();
										u.setTitle(js.getString("title"));
										u.setContent(js.getString("content"));
										u.setPublishUser(js
												.getString("publishUser"));
										u.setPubshTime(js
												.getString("pubshTime"));
										u.setImg(js.getString("img"));
										u.setAvailabilityTime(js
												.getString("availabilityTime"));
										u.setUuid(js.getString("id"));
										u.setSort(js.getInt("sort"));
										noticedao.insert(u);

										listadpter.addItem(u);
										listadpter.notifyDataSetChanged();

									} catch (Exception e) {
										e.printStackTrace();
									}
								} else {

								}
							}
						});
			}

		}

		Iterator<String> iter = map.keySet().iterator();
		if (map != null) {
			while (iter.hasNext()) {
				String key = iter.next();
				String value = map.get(key);
				noticedao.delete(value);
			}
		}

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		List<NewsContent> list2 = noticedao.findByList();
		listadpter.clear();
		for (int i = 0; i < list2.size(); i++) {
			NewsContent u = list2.get(i);

			listadpter.addItem(u);
		}
		listadpter.notifyDataSetChanged();
		super.onResume();
	}

}
